package com.will.txj.aj.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.will.txj.aj.global.utils.StringRedisUtil;
import com.will.txj.aj.server.client.WYServerClient;
import com.will.txj.aj.server.utils.MutableHttpServletRequest;
import com.will.txj.common.result.R;
import com.will.txj.common.security.AJUser;
import com.will.txj.common.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author: wen-yi;
 * @date: 2021/11/29 17:14;
 * @Description:
 */
@Slf4j
@Api(tags = "应用控制层")
@RestController
@RequestMapping("app")
public class APPController {
    @Autowired
    private WYServerClient wyServerClient;
    @Autowired
    private StringRedisUtil stringRedisUtil;
    @Autowired
    private RestTemplate restTemplate;

    @ApiOperation("调用:登录信息")
    @PostMapping
    public R hello(){
        log.info("调用:登录信息");
        /*
        AJUser ajUser = (AJUser) authentication.getPrincipal();
        String jwt = stringRedisUtil.get("token-" + ajUser.getUsername());
        if (StringUtil.isNotEmptyToString(jwt)){
            JSONObject jsonObject = JSONObject.parseObject(jwt, JSONObject.class);
            String token = (String) jsonObject.get("access_token");
            //log.info("获取到的token令牌 -> token: {}",token);
            String url = "http://localhost:11010/app";
            HttpHeaders headers = new HttpHeaders();
            // headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            // headers.setBearerAuth(token);
            headers.set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJiaXJ0aGRheSI6bnVsbCwidUlkIjoyLCJhdWQiOlsiYWpfc2VydmVyIl0sInVzZXJfbmFtZSI6ImFqIiwic2NvcGUiOlsiYWxsIl0sInNleCI6bnVsbCwibmlja25hbWUiOiLnnaHop4kiLCJleHAiOjE2MzkwOTU0ODUsImF1dGhvcml0aWVzIjpbInNob3ciLCJST0xFX3VzZXIiXSwianRpIjoiZjI2ZWQ2MDEtNjU1ZS00MGM0LWExNDMtYTE3MzQzMDI0YzI0IiwiY2xpZW50X2lkIjoidXNlciIsInVzZXJuYW1lIjoiYWoifQ.sv0PyWe3zldfnp6POR9wLIhZIbNvGDZwoGnAV0jJ8VY");
            log.info("url: {}, header: {}",url,headers);
            Object res = restTemplate.postForObject(url, headers, Object.class);
            log.info("获取的响应:{}",res);
            return R.ok(res);
        }
        return R.fail();
         */
        return wyServerClient.hello();
    }

    @ApiOperation("调用:角色验证")
    @PreAuthorize("hasRole('user')")
    @GetMapping("role")
    public R role(){
        log.info("调用:角色信息");
        return wyServerClient.role();
    }

    @ApiOperation("调用:权限验证")
    @PreAuthorize("hasAuthority('show')")
    @GetMapping("调用:permission")
    public R permission(){
        log.info("调用:权限信息");
        return wyServerClient.permission();
    }
}
